<?php
/**
 * 囤福家园
 *
 * @copyright © 2022 by 技术先锋 All rights reserved。
 * @Created   by PhpStorm.
 * @author    StarsPhp
 * @date      2023/1/18
 * @time      14:20
 */
declare (strict_types=1);

namespace app\command\workerman\crontab;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use Workerman\Worker;
use Workerman\Crontab\Crontab;

class CrontabDemo extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('CrontabDemo')
            ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
            ->addOption('mode', 'm', Option::VALUE_OPTIONAL, 'Run the workerman server in daemon mode.')
            ->setDescription('CrontabDemo Server for ThinkPHP the workerman command');
        
        // $this->setName('worker:gateway')
        //     ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
        //     ->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server.', null)
        //     ->addOption('port', 'p', Option::VALUE_OPTIONAL, 'the port of workerman server.', null)
        //     ->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the workerman server in daemon mode.')
        //     ->setDescription('GatewayWorker Server for ThinkPHP');
    }
    
    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('convert start');
        
        $action = $input->getArgument('action');
        $mode = $input->getOption('mode');
        
        
        // 重新构造命令行参数,以便兼容workerman的命令
        global $argv;
        
        $argv = [];
        
        array_unshift($argv, 'think', $action);
        if ($mode == 'd') {
            $argv[] = '-d';
        } else if ($mode == 'g') {
            $argv[] = '-g';
        }
        
        
        // if (strpos(strtolower(PHP_OS), 'win') === 0) {
        //     exit("start.php not support windows, please use start_for_win.bat\n");
        // }
        
        // 检查扩展
        // if (!extension_loaded('pcntl')) {
        //     exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
        // }
        //
        // if (!extension_loaded('posix')) {
        //     exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
        // }
        
        // 标记是全局启动
        define('GLOBAL_START', 1);
        
        
        // foreach (glob(app()->getBasePath() . '/im/start*.php') as $start_file) {
        //     require_once $start_file;
        // }

        $worker = new Worker();

// 设置时区，避免运行结果与预期不一致
        date_default_timezone_set('PRC');
    
        $worker->onWorkerStart = function () {
            // 每分钟的第1秒执行.
            new Crontab('*/1 * * * * *', function(){
                echo date('Y-m-d H:i:s')."\n";
            });
            // 每天的7点50执行，注意这里省略了秒位.
            // new Crontab('50 7 * * *', function(){
            //     echo date('Y-m-d H:i:s')."\n";
            // });
        };
        
        Worker::runAll();
    }

}
